/**********************************************************
 * InitializeJobs.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 11:04:07 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.servlet;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

import com.printeverywhere.action.ProcessClosedAuctionsAction;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.BackgroundProcess;
import com.printeverywhere.job.ProcessBannerHourlyStatisticsJob;
import com.printeverywhere.job.ProcessEndedAuctionsJob;
import com.printeverywhere.job.ProcessGenerateInvoicesJob;
import com.printeverywhere.job.PrunePendingItemsJob;
import com.printeverywhere.job.PrunePendingUserJob;
import com.printeverywhere.util.AdminConstants;

/**
 * The Class InitializeJobs.
 */
public class InitializeJobs extends HttpServlet {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(InitializeJobs.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 *
	 * @see javax.servlet.GenericServlet#init()
	 */
	public void init() {
		if (logger.isDebugEnabled()) {
			logger.debug("init() - start"); //$NON-NLS-1$
		}

		try {
			 // ActionProxy proxy = .createActionProxy("",
				// context.getJobDetail().getName(), ctx);
			 List<BackgroundProcess> backgroundProcessList = (List<BackgroundProcess>) CommonFunctionDelegator.getAllfromtable(BackgroundProcess.class.getName());
			 if(backgroundProcessList != null && backgroundProcessList.size() > 0){
				 for (Iterator<BackgroundProcess> iterator = backgroundProcessList.iterator(); iterator
						.hasNext();) {
					 BackgroundProcess backgroundProcess = (BackgroundProcess) iterator
							.next();
					 if(backgroundProcess.getName().equals("Prune users")){
						 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler ();
						 JobDetail jobDetail = new JobDetail ( "test", Scheduler.DEFAULT_GROUP, PrunePendingUserJob.class);
						 long ctime = System.currentTimeMillis ();
						 SimpleTrigger simpleTrigger = new SimpleTrigger ( " simpleTrigger "," triggerGroup-s1 ");
						 simpleTrigger.setStartTime (new Date (ctime));
						 simpleTrigger.setRepeatInterval(backgroundProcess.getUpdateTime() * 60 * 1000);
//						 simpleTrigger.setRepeatCount(100);
						 scheduler.scheduleJob(jobDetail, simpleTrigger);
						 scheduler.start ();
					 }else if(backgroundProcess.getName().equals("Prune items")){
						 // ActionProxy proxy = .createActionProxy("",
						 // context.getJobDetail().getName(), ctx);
						 Scheduler pruneItemsScheduler = StdSchedulerFactory.getDefaultScheduler ();
						 JobDetail pruneItemsJobDetail = new JobDetail ( "testItems", Scheduler.DEFAULT_GROUP, PrunePendingItemsJob.class);
						 long pruneItemstime = System.currentTimeMillis ();
						 SimpleTrigger pruneItemsTrigger = new SimpleTrigger ( " pruneItemsTrigger "," triggerGroup-s1 ");
						 pruneItemsTrigger.setStartTime (new Date (pruneItemstime));
						 pruneItemsTrigger.setRepeatInterval(backgroundProcess.getUpdateTime() * 60 * 1000);
						 //pruneItemsTrigger.setRepeatCount(100);
						 pruneItemsScheduler.scheduleJob(pruneItemsJobDetail, pruneItemsTrigger);
						 pruneItemsScheduler.start();
					 }else if(backgroundProcess.getName().equals("Process ended auctions")){
						 Scheduler processEndedAuctionsScheduler = StdSchedulerFactory.getDefaultScheduler();
						 JobDetail processEndedAuctionsJobDetail = new JobDetail ( "testEndedAuctions", Scheduler.DEFAULT_GROUP, ProcessEndedAuctionsJob.class);
						 long processEndedAuctionstime = System.currentTimeMillis ();
						 SimpleTrigger processEndedAuctionsTrigger = new SimpleTrigger ( " processEndedAuctionsTrigger "," triggerGroup-s1 ");
						 processEndedAuctionsTrigger.setStartTime (new Date (processEndedAuctionstime));
						 processEndedAuctionsTrigger.setRepeatInterval(backgroundProcess.getUpdateTime() * 60 * 1000);
//						 processEndedAuctionsTrigger.setRepeatCount(100);
						 processEndedAuctionsScheduler.scheduleJob(processEndedAuctionsJobDetail, processEndedAuctionsTrigger);
						 processEndedAuctionsScheduler.start();
					 }else if(backgroundProcess.getName().equals("Generate invoices")){
						 Scheduler generateInvoicesScheduler = StdSchedulerFactory.getDefaultScheduler();
						 JobDetail generateInvoicesJobDetail = new JobDetail ( "testgenerateInvoices", Scheduler.DEFAULT_GROUP, ProcessGenerateInvoicesJob.class);
						 long generateInvoicesTime = System.currentTimeMillis();
						 SimpleTrigger generateInvoicesTrigger = new SimpleTrigger ( " generateInvoicesTrigger "," triggerGroup-s1 ");
						 generateInvoicesTrigger.setStartTime (new Date (generateInvoicesTime));
						 generateInvoicesTrigger.setRepeatInterval(backgroundProcess.getUpdateTime() * 60 * 1000);
//						 processEndedAuctionsTrigger.setRepeatCount(100);
						 generateInvoicesScheduler.scheduleJob(generateInvoicesJobDetail, generateInvoicesTrigger);
						 generateInvoicesScheduler.start();
					 }
					 Scheduler generateBannerHourlyStatistics = StdSchedulerFactory.getDefaultScheduler();
					 JobDetail generateBannerHourlyJobDetail = new JobDetail ( "testgenerateInvoices", Scheduler.DEFAULT_GROUP, ProcessBannerHourlyStatisticsJob.class);
					 long generateBannerHourlyStatisticsTime = System.currentTimeMillis();
					 SimpleTrigger generateBannerHourlyStatisticsTrigger = new SimpleTrigger ( " generateInvoicesTrigger "," triggerGroup-s1 ");
					 generateBannerHourlyStatisticsTrigger.setStartTime (new Date (generateBannerHourlyStatisticsTime));
					 generateBannerHourlyStatisticsTrigger.setRepeatInterval(60 * 60 * 1000);
//					 processEndedAuctionsTrigger.setRepeatCount(100);
					 generateBannerHourlyStatistics.scheduleJob(generateBannerHourlyJobDetail, generateBannerHourlyStatisticsTrigger);
					 generateBannerHourlyStatistics.start();

				 }
			 }
		 } catch (Exception e) {
			logger.error("init()", e); //$NON-NLS-1$
			 e.printStackTrace();
		 }

		if (logger.isDebugEnabled()) {
			logger.debug("init() - end"); //$NON-NLS-1$
		}
	}
}